package com.salt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.salt.entity.IdleAffiliateCpsIncomeDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 闲鱼联盟CPS佣金明细Mapper
 */
@Mapper
public interface IdleAffiliateCpsIncomeDetailMapper extends BaseMapper<IdleAffiliateCpsIncomeDetail> {
    
    /**
     * 统计指定日期范围内的佣金明细汇总数据
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param inviteCode
     * @return 汇总数据，包含：
     *         - hasCouponCount: 有优惠券的数量
     *         - noCouponCount: 无优惠券的数量
     *         - totalAssessAmount: 预估金额总和
     */
    @Select("<script>" +
            "SELECT " +
            "COUNT(CASE WHEN order_coupon_discount_fee &gt;= 3 THEN 1 END) as hasCouponCount, " +
            "COUNT(CASE WHEN order_coupon_discount_fee &lt; 3 OR order_coupon_discount_fee IS NULL THEN 1 END) as noCouponCount, " +
            "COALESCE(SUM(assess_amount), 0) as totalAssessAmount " +
            "FROM idle_affiliate_cps_income_detail " +
            "WHERE 1=1 " +
            "AND bill_state != 3 " +
            "AND sub_publisher_id = #{inviteCode} " +
            "<if test='startDate != null'> AND order_create_time &gt;= #{startDate} </if>" +
            "<if test='endDate != null'> AND order_create_time &lt;= #{endDate} </if>" +
            "</script>")
    Map<String, Object> getIncomeSummary(@Param("startDate") Date startDate, @Param("endDate") Date endDate, @Param("inviteCode") String inviteCode);

    /**
     * 分页查询佣金明细数据
     * @param page 分页参数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param billState 账单状态
     * @param orderState 订单状态
     * @param hasCoupon 是否有优惠券
     * @param inviteCode
     * @param keyword 关键词（商品标题或订单ID）
     * @return 分页数据
     */
    @Select("<script>" +
            "SELECT * FROM idle_affiliate_cps_income_detail " +
            "WHERE 1=1 " +
            " AND bill_state != 3 "+
            " AND sub_publisher_id = #{inviteCode}  "+
            "<if test='startDate != null'> AND order_create_time &gt;= #{startDate} </if>" +
            "<if test='endDate != null'> AND order_create_time &lt;= #{endDate} </if>" +
            "<if test='billState != null'> AND bill_state = #{billState} </if>" +
            "<if test='orderState != null'> AND order_state = #{orderState} </if>" +
            "<if test='hasCoupon != null'> " +
            "   <choose>" +
            "       <when test='hasCoupon'> AND order_coupon_discount_fee &gt; 0 </when>" +
            "       <otherwise> AND (order_coupon_discount_fee = 0 OR order_coupon_discount_fee IS NULL) </otherwise>" +
            "   </choose>" +
            "</if>" +
            "<if test='keyword != null and keyword != \"\"'> " +
            "   AND (item_title LIKE CONCAT('%', #{keyword}, '%') OR order_id LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "ORDER BY order_create_time DESC" +
            "</script>")
    IPage<IdleAffiliateCpsIncomeDetail> pageQuery(
            Page<IdleAffiliateCpsIncomeDetail> page,
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate,
            @Param("billState") Integer billState,
            @Param("orderState") Integer orderState,
            @Param("hasCoupon") Boolean hasCoupon, @Param("keyword") String keyword,
            @Param("inviteCode")String inviteCode);
} 